﻿using System;
using System.IO;

namespace CJ_Logging
{
    public class LogHelper
    {
        private static LogHelper m_LogHelper = null;
        public static LogHelper Instance
        {
            get
            {
                if (m_LogHelper == null)
                {
                    m_LogHelper = new LogHelper();
                    m_LogHelper.SetConfig();
                }
                return m_LogHelper;
            }
        }
        /// <summary>
        /// 记录程序日志
        /// </summary>
        private readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("InfoLogger"); 

        /// <summary>
        /// 记录异常
        /// </summary>
        private readonly log4net.ILog logerror = log4net.LogManager.GetLogger("ExceptionLogger");

        private void SetConfig()
        {
            log4net.Config.XmlConfigurator.Configure();
        }

        private void SetConfig(FileInfo configFile)
        {
            log4net.Config.XmlConfigurator.Configure(configFile);
        }

        /// <summary>
        /// 记录程序日志
        /// </summary>
        /// <param name="info"></param>
        public void WriteLog(string info)
        {
            if (loginfo.IsInfoEnabled)
            {
                info = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ms") + "\r\n" + info;
                loginfo.Info(info);
            }
        }

        /// <summary>
        /// 记录程序错误和异常
        /// </summary>
        /// <param name="error"></param>
        public void WriteError(string error)
        {
            if (logerror.IsInfoEnabled)
            {
                error = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ms") + "\r\n" + error;
                logerror.Error(error);
            }
        }

        /// <summary>
        /// 记录程序错误和异常
        /// </summary>
        /// <param name="error"></param>
        /// <param name="se"></param>
        public void WriteError(string error, Exception se)
        {
            if (logerror.IsErrorEnabled)
            {
                error = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ms") + "\r\n" + error;
                logerror.Error(error, se);
            }
        }
    }
}